<template>
  <div class="login_container">
    <div class="login_box">
      <div class="avatar_box"><img src="../assets/logo.png" alt="" /></div>
      <!-- 登录表单区域 -->
      <el-form
        ref="loginFormRef"
        label-width="0px"
        :model="loginForm"
        :rules="loginFormRules"
        class="login_form"
      >
        <!-- 账户 -->
        <el-form-item label="" prop="username">
          <el-input
            prefix-icon="el-icon-user"
            v-model="loginForm.username"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item label="" prop="password">
          <el-input
            prefix-icon="el-icon-lock"
            v-model="loginForm.password"
            show-password
            @keyup.enter.native="login"
          ></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        username: "touseiin",
        password: "hifuclub",
      },
      loginFormRules: {
        username: [
          { required: true, message: "请输入登录账户 ", trigger: "blur" },
          { min: 4, max: 11, message: "长度在 4 到 11个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入登录密码 ", trigger: "blur" },
          { min: 5, max: 15, message: "长度在 5 到 15个字符", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    // 重置按钮
    resetLoginForm() {
      console.log(this);
      this.$refs.loginFormRef.resetFields();
    },

    // 登录按钮
    login() {
      this.$refs.loginFormRef.validate(async (valid) => {
        console.log(valid);
        if (!valid) return;
        const { data: res } = await this.$http.post("login", this.loginForm);
        // console.log(res);
        if (res.meta.status !== 200)
          // return console.log("登录失败");
          return this.$message.error("登录失败");
        if (res.meta.status == 200) {
          console.log(res);
          // 把sessionStorage用户登录的信息setItem住
          window.sessionStorage.setItem("token", res.data.token);
          //  this.$router.push({path:'/home'})
          this.$router.push("/home"); //也可以这样写
          return this.$message.success("登录成功");
        }
        // return console.log("登陆成功");
      });
    },
  },
};
</script>

<style lang="less" scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
}
.login_box {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: 450px;
  height: 300px;
  background-color: #fff;
  .avatar_box {
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 130px;
    height: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    overflow: hidden;
    padding: 10px;
    background-color: #fff;
    box-shadow: 0 0 10px #ddd;

    img {
      border-radius: 50%;
      width: 100%;
      height: 100%;
      background-color: #eee;
    }
  }
  .btns {
    display: flex;
    justify-content: flex-end;
  }
  .login_form {
    margin-top: 100px;
    padding: 0 10px;
  }
}
</style>